package com.xy.blog.user.dao.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xy.blog.core.dao.IBaseCommonDao;
import com.xy.blog.core.exception.BaseException;
import com.xy.blog.core.id.SnowflakeIdWorker;
import com.xy.blog.core.springmvc.result.page.PageReq;
import com.xy.blog.core.springmvc.result.page.PageResp;
import com.xy.blog.user.controller.model.req.query.AttrDictQuery;
import com.xy.blog.user.dao.mapper.AttrDictMapper;
import com.xy.blog.user.dao.model.AttrDictDO;
import com.xy.blog.user.exception.constants.AttrDictErrorCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zanhonglei
 * @version V1.0
 * @Description:
 * @date 2020/3/5
 */
@Repository
public class AttrDictDaoImpl implements IBaseCommonDao<AttrDictDO, AttrDictQuery> {

    @Autowired
    private AttrDictMapper attrDictMapper;

    @Override
    public PageResp<AttrDictDO> listByPage(PageReq<AttrDictQuery> req) throws BaseException {
        Page<Object> objects = PageHelper.startPage(req);
        AttrDictQuery params = req.getParams();
        Example example = new Example(AttrDictDO.class);
        Example.Criteria criteria = example.createCriteria();
        if (params != null && StringUtils.isNotEmpty(params.getName())) {
            criteria.andLike("name", "%" + params.getName().trim() + "%");
        }
        List<AttrDictDO> articleDOS = attrDictMapper.selectByExample(example);
        return PageResp.data(articleDOS,objects);
    }

    @Override
    public List<AttrDictDO> select(AttrDictDO item) {
        Example example = new Example(AttrDictDO.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(item.getName())) {
            criteria.andLike("name", "%" + item.getName().trim() + "%");
        }
        return attrDictMapper.selectByExample(example);
    }

    @Override
    public int selectCount(AttrDictDO item) {
        return 0;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void save(AttrDictDO item) throws BaseException {
        if (item.getId() == null) {
            item.setId(SnowflakeIdWorker.generateId());
        }
        if (item.getCreateTime() == null) {
            item.setCreateTime(LocalDateTime.now());
        }
        if (item.getUpdateTime() == null) {
            item.setUpdateTime(LocalDateTime.now());
        }
        AttrDictDO query = new AttrDictDO();
        query.setCode(item.getCode());
        if (attrDictMapper.selectCount(query) > 0) {
            throw new BaseException(AttrDictErrorCode.USER_ATTR_EXITS);
        }
        attrDictMapper.insert(item);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void updateById(AttrDictDO item) {
        AttrDictDO query = new AttrDictDO();
        query.setCode(item.getCode());
        if (attrDictMapper.selectCount(query) > 0) {
            throw new BaseException(AttrDictErrorCode.USER_ATTR_EXITS);
        }
        attrDictMapper.updateByPrimaryKeySelective(item);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void deleteById(Long id) {
        attrDictMapper.deleteByPrimaryKey(id);
    }

    @Override
    public AttrDictDO getById(Long id) {
        return attrDictMapper.selectByPrimaryKey(id);
    }
}
